package com.le.tester.book.effectivejava3rdchinese.secondReading.l28;

import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * createTime：2024-09-06 14:05
 * description：object[]
 */
public class Chooser {
    //不要这么做，在做类型转换的时候可能会classCastException()，不稳定因素
    private final Object[] choiceArray;

    public Chooser(Collection choice) {
        choiceArray = choice.toArray();
    }

    public Object choose() {
        Random random = ThreadLocalRandom.current();

        return choiceArray[random.nextInt(choiceArray.length)];
    }
}
